<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    
    
    public function __construct(){
        parent::__construct();
        
        
        $this->model = $this->getModel();
        $this->aut = new bgAuthorize();
        
    }
    
    public function display($cachable = false, $urlparams = false) {
        parent::display($cachable, $urlparams);
    }
    
    /**
     * Remove image when change file
    **/
    public function removeImage(){
        $image_name = JRequest::getVar('image_name');
        if($image_name != null){
            $path = "uploads/images/com_pgb/$image_name";
            if(file_exists($path)){
                $del = unlink($path);
            }
        }
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    
    /* save temp pool, gym, boat */
    public function savetemp(){
        /* get data from submit form */
        $data = &JRequest::getVar('jform');
        $action = JRequest::getVar('action');
        if($action == "new"){
            $addNew = true;
        }else{
            $addNew = false;
        }
        /**
         * GET data for POOL
        **/        
        //Check null or 0 for Money
        $data['POOL_CIAD'] = $data['POOL_CIAD']==='' ? null : $data['POOL_CIAD'];
        $data['POOL_CICI'] = $data['POOL_CICI']==='' ? null : $data['POOL_CICI'];
        $data['POOL_COAD'] = $data['POOL_COAD']==='' ? null : $data['POOL_COAD'];
        $data['POOL_COCI'] = $data['POOL_COCI']==='' ? null : $data['POOL_COCI'];
        
        
        
        /* get lis extend Facility */
        $extendFac = $this->model->getAllExtendFac();
        /* checkbox extend facility pool */
        $list_tbpo = '';
        foreach($extendFac as $value){
            if(isset($_POST['cbfac'.$value->TBPO_CODE])){
                $list_tbpo .= ",".$_POST['cbfac'.$value->TBPO_CODE];
            }
        }
        $list_tbpo = substr($list_tbpo,1);
        $data['POOL_TBPO_CODE_LIST'] = $list_tbpo;
        
        /* get list disabled for pool */
        $disabled_pool = $this->model->getBarrierFreeList();
        /* checkbox disabled pool */
        $list_disp_pool = '';        
        foreach($disabled_pool as $value){
            if(isset($_POST['cbdis'.$value->DISP_CODE])){
                $list_disp_pool .= ",".$_POST['cbdis'.$value->DISP_CODE];
            }
        }
        $list_disp_pool = substr($list_disp_pool,1);
        $data['POOL_DISP_CODE_LIST'] = $list_disp_pool;
        
        
        
        /**
         * GET data for GYMT
         * */
        //Check null or 0 for Money
        
        $data['GYMT_CIAD1'] = $data['GYMT_CIAD1']==='' ? null : $data['GYMT_CIAD1'];
        $data['GYMT_CICI1'] = $data['GYMT_CICI1']==='' ? null : $data['GYMT_CICI1'];
        $data['GYMT_COAD1'] = $data['GYMT_COAD1']==='' ? null : $data['GYMT_COAD1'];
        $data['GYMT_COCI1'] = $data['GYMT_COCI1']==='' ? null : $data['GYMT_COCI1'];
        
        $data['GYMT_CIAD2'] = $data['GYMT_CIAD2']==='' ? null : $data['GYMT_CIAD2'];
        $data['GYMT_CICI2'] = $data['GYMT_CICI2']==='' ? null : $data['GYMT_CICI2'];
        $data['GYMT_COAD2'] = $data['GYMT_COAD2']==='' ? null : $data['GYMT_COAD2'];
        $data['GYMT_COCI2'] = $data['GYMT_COCI2']==='' ? null : $data['GYMT_COCI2'];
        
        
        
        
        /* checkbox gymt */
        
        /* checkbox gymt 1 */
        
        /* get list disabled gymt */
        $disabled_gymt = $this->model->getGymtBarrierFreeList();;
        
        $list_disabled_gymt = '';
        foreach($disabled_gymt as $value){
            if(isset($_POST['cbdisgym'.$value->TBGY_CODE])){
                $list_disabled_gymt .= ",".$_POST['cbdisgym'.$value->TBGY_CODE];
            }
        }
        
        $list_disabled_gymt = substr($list_disabled_gymt,1);
        $data['GYMT_TBGY_CODE_LIST1'] = $list_disabled_gymt;
        $data['GYMT_TBGY_CODE_LIST2'] = null;
        
        
        
        /**
         * GET data for DOCK
         **/
         //Check null or 0 for Money
        $data['DOCK_CIAD'] = $data['DOCK_CIAD']==='' ? null : $data['DOCK_CIAD'];
        $data['DOCK_CICI'] = $data['DOCK_CICI']==='' ? null : $data['DOCK_CICI'];
        $data['DOCK_COAD'] = $data['DOCK_COAD']==='' ? null : $data['DOCK_COAD'];
        $data['DOCK_COCI'] = $data['DOCK_COCI']==='' ? null : $data['DOCK_COCI'];
        
        
        
        /* get list disabled dock */
        $disabled_dock = $this->model->getDockBarrierFreeList();;
        
        /* checkbox dock */
        $list_disabled_dock = '';
        foreach($disabled_dock as $value){
            if(isset($_POST['cbdock'.$value->TBDO_CODE])){
                $list_disabled_dock .= ",".$_POST['cbdock'.$value->TBDO_CODE];
            }
        }
        
        $list_disabled_dock = substr($list_disabled_dock,1);
        $data['DOCK_TBDO_CODE_LIST'] = $list_disabled_dock;
        
        /* get image */
        
        
        if($addNew == true){
            if($data['DOCK_IMGD_NEW'] != null){
                $data['DOCK_IMGD'] = $data['DOCK_IMGD_NEW'];
            }
            if($data['DOCK_IMGW_NEW'] != null){
                    $data['DOCK_IMGW'] = $data['DOCK_IMGW_NEW'];
            }
            if($data['DOCK_IMGS_NEW'] != null){
                    $data['DOCK_IMGS'] = $data['DOCK_IMGS_NEW'];
            }
        }else{
            /* IMAGE 1 */
            if($data['DOCK_IMGD_NEW'] != null){
                $data['DOCK_IMGD'] = $data['DOCK_IMGD_NEW'];
            }else{
                if($data['DOCK_IMGD_OLD'] != null){
                    $data['DOCK_IMGD'] = $data['DOCK_IMGD_OLD'];
                }
            }
            /* IMAGE 2 */
            if($data['DOCK_IMGW_NEW'] != null){
                $data['DOCK_IMGW'] = $data['DOCK_IMGW_NEW'];
            }else{
                if($data['DOCK_IMGW_OLD'] != null){
                    $data['DOCK_IMGW'] = $data['DOCK_IMGW_OLD'];
                }
            }
            /* IMAGE 3 */
            if($data['DOCK_IMGS_NEW'] != null){
                $data['DOCK_IMGS'] = $data['DOCK_IMGS_NEW'];
            }else{
                if($data['DOCK_IMGS_OLD'] != null){
                    $data['DOCK_IMGS'] = $data['DOCK_IMGS_OLD'];
                }
            }
        }
        
                
        /**
         * check data
        **/
        if(empty($data['OCBA_CODE'])){
            $error = "センターを選択してください。";
            JRequest::setVar('task','error');
            JFactory::getApplication()->enqueueMessage($error, 'error');
                    
        }else{
            if(!$this->model->checkExistOCBA_CODE($data['OCBA_CODE'])){
                //$error = "KHONG TIM THAY TRUNG TAM NAY";
                $error = "このセンターは見つかれません。";
                JRequest::setVar('task','error');
                JFactory::getApplication()->enqueueMessage($error, 'error');
                
                parent::display();
                return;
            }
            /**
             * save data
            **/
            $status = $this->model->savetemp($data, $addNew);
            if($status === true){
                
                
                $this->setRedirect('index.php?option=com_pgb');
            }else{
                if($status == 'empty'){
                    //required atleast 1 value
                    $error = "何も登録情報がありません";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
                } else {
                    //JError::raiseError(500, 'Error ! Can\'t save' );
                    $error = "予期せぬエラーが発生しました。登録ができませんでした。";
                    JRequest::setVar('task','');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
                }
                
            }
            
        }
        parent::display();
    }
    
    /**
     * save pool, gym, boat 
    **/
    public function save(){
        /* get data from submit form */
        $data = &JRequest::getVar('jform');
        $action = JRequest::getVar('action');
        if($action == "new"){
            $addNew = true;
        }else{
            $addNew = false;
        }

        /**
         * GET data for POOL
        **/        
        //Check null or 0 for Money
        $data['POOL_CIAD'] = $data['POOL_CIAD']==='' ? null : $data['POOL_CIAD'];
        $data['POOL_CICI'] = $data['POOL_CICI']==='' ? null : $data['POOL_CICI'];
        $data['POOL_COAD'] = $data['POOL_COAD']==='' ? null : $data['POOL_COAD'];
        $data['POOL_COCI'] = $data['POOL_COCI']==='' ? null : $data['POOL_COCI'];
        
        
        
        
        
        /* get lis extend Facility */
        $extendFac = $this->model->getAllExtendFac();
        /* checkbox extend facility pool */
        $list_tbpo = '';
        foreach($extendFac as $value){
            if(isset($_POST['cbfac'.$value->TBPO_CODE])){
                $list_tbpo .= ",".$_POST['cbfac'.$value->TBPO_CODE];
            }
        }
        $list_tbpo = substr($list_tbpo,1);
        $data['POOL_TBPO_CODE_LIST'] = $list_tbpo;
        
        /* get list disabled for pool */
        $disabled_pool = $this->model->getBarrierFreeList();
        /* checkbox disabled pool */
        $list_disp_pool = '';
        foreach($disabled_pool as $value){
            if(isset($_POST['cbdis'.$value->DISP_CODE])){
                $list_disp_pool .= ",".$_POST['cbdis'.$value->DISP_CODE];
            }
        }
        $list_disp_pool = substr($list_disp_pool,1);
        $data['POOL_DISP_CODE_LIST'] = $list_disp_pool;
        
        /**
         * GET data for GYMT
         * */
        //Check null or 0 for Money
        
        $data['GYMT_CIAD1'] = $data['GYMT_CIAD1']==='' ? null : $data['GYMT_CIAD1'];
        $data['GYMT_CICI1'] = $data['GYMT_CICI1']==='' ? null : $data['GYMT_CICI1'];
        $data['GYMT_COAD1'] = $data['GYMT_COAD1']==='' ? null : $data['GYMT_COAD1'];
        $data['GYMT_COCI1'] = $data['GYMT_COCI1']==='' ? null : $data['GYMT_COCI1'];
        
        $data['GYMT_CIAD2'] = $data['GYMT_CIAD2']==='' ? null : $data['GYMT_CIAD2'];
        $data['GYMT_CICI2'] = $data['GYMT_CICI2']==='' ? null : $data['GYMT_CICI2'];
        $data['GYMT_COAD2'] = $data['GYMT_COAD2']==='' ? null : $data['GYMT_COAD2'];
        $data['GYMT_COCI2'] = $data['GYMT_COCI2']==='' ? null : $data['GYMT_COCI2'];
        
        
        
        
        /* checkbox gymt */
                        
        /* checkbox gymt 1 */
        
        /* get list disabled gymt */
        $disabled_gymt = $disabled_pool;
        
        $list_disabled_gymt = '';
        foreach($disabled_gymt as $value){
            if(isset($_POST['cbdisgym'.$value->DISP_CODE])){
                $list_disabled_gymt .= ",".$_POST['cbdisgym'.$value->DISP_CODE];
            }
        }
        
        $list_disabled_gymt = substr($list_disabled_gymt,1);
        $data['GYMT_TBGY_CODE_LIST1'] = $list_disabled_gymt;
        $data['GYMT_TBGY_CODE_LIST2'] = null;
        
        
        
        /**
         * GET data for DOCK
         **/
         //Check null or 0 for Money
        $data['DOCK_CIAD'] = $data['DOCK_CIAD']==='' ? null : $data['DOCK_CIAD'];
        $data['DOCK_CICI'] = $data['DOCK_CICI']==='' ? null : $data['DOCK_CICI'];
        $data['DOCK_COAD'] = $data['DOCK_COAD']==='' ? null : $data['DOCK_COAD'];
        $data['DOCK_COCI'] = $data['DOCK_COCI']==='' ? null : $data['DOCK_COCI'];
        
        
        
        
        
        
        /* get list disabled dock */
        $disabled_dock = $disabled_pool;
        
        /* checkbox dock */
        $list_disabled_dock = '';
        foreach($disabled_dock as $value){
            if(isset($_POST['cbdock'.$value->DISP_CODE])){
                $list_disabled_dock .= ",".$_POST['cbdock'.$value->DISP_CODE];
            }
        }
        
        $list_disabled_dock = substr($list_disabled_dock,1);
        $data['DOCK_TBDO_CODE_LIST'] = $list_disabled_dock;
        
        /* get image */
        
        
        if($addNew == true){
            if($data['DOCK_IMGD_NEW'] != null){
                $data['DOCK_IMGD'] = $data['DOCK_IMGD_NEW'];
            }
            if($data['DOCK_IMGW_NEW'] != null){
                    $data['DOCK_IMGW'] = $data['DOCK_IMGW_NEW'];
            }
            if($data['DOCK_IMGS_NEW'] != null){
                    $data['DOCK_IMGS'] = $data['DOCK_IMGS_NEW'];
            }
        }else{
            /* IMAGE 1 */
            if($data['DOCK_IMGD_NEW'] != null){
                $data['DOCK_IMGD'] = $data['DOCK_IMGD_NEW'];
            }else{
                if($data['DOCK_IMGD_OLD'] != null){
                    $data['DOCK_IMGD'] = $data['DOCK_IMGD_OLD'];
                }
            }
            /* IMAGE 2 */
            if($data['DOCK_IMGW_NEW'] != null){
                $data['DOCK_IMGW'] = $data['DOCK_IMGW_NEW'];
            }else{
                if($data['DOCK_IMGW_OLD'] != null){
                    $data['DOCK_IMGW'] = $data['DOCK_IMGW_OLD'];
                }
            }
            /* IMAGE 3 */
            if($data['DOCK_IMGS_NEW'] != null){
                $data['DOCK_IMGS'] = $data['DOCK_IMGS_NEW'];
            }else{
                if($data['DOCK_IMGS_OLD'] != null){
                    $data['DOCK_IMGS'] = $data['DOCK_IMGS_OLD'];
                }
            }
        }
        
        /**
         * check data
        **/

        if(empty($data['OCBA_CODE'])){
            //$error = "VUI LONG CHON TRUNG TAM";
            $error = "センター・クラブを選択してください。";
            JRequest::setVar('task','error');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        // }
        // elseif(empty($data['POOL_ADMN']) && empty($data['GYMT_ADMN1']) && empty($data['DOCK_ADMN'])){
        //     //$error = "VUI LONG NHAP TEN NHA THAU QUAN LY";
        //     $error = "「運営委任業者」に入力してください。";
        //     JRequest::setVar('task','error');
        //     JFactory::getApplication()->enqueueMessage($error, 'error');
        // }
        // /**/
        // elseif(!empty($data['POOL_ADMN']) && empty($data['POOL_TERM'])){            
        //     $error = "開館期間を入力してください。";
        //     JRequest::setVar('task','error');
        //     JFactory::getApplication()->enqueueMessage($error, 'error');
        // }
        // /**/
        // elseif(!empty($data['GYMT_ADMN1']) && ($data['GYMT_2NDK'] == 2) && empty($data['GYMT_ADMN2'])){
        //         //$error = "BAN CHUA NHAP TEN PHONG TAP GYMT THU 2";
        //         $error = "二つ目の体育館名はまだ入力されていません。";
        //         JRequest::setVar('task','error');
        //         JFactory::getApplication()->enqueueMessage($error, 'error');
            
        }
        else{
            /* check OCBA_CODE exist */
            if(!$this->model->checkExistOCBA_CODE($data['OCBA_CODE'])){
                //$error = "KHONG TIM THAY TRUNG TAM NAY";
                $error = "このセンター・クラブは見つかれません。";
                JRequest::setVar('task','error');
                JFactory::getApplication()->enqueueMessage($error, 'error');
                
                parent::display();
                return;
            }
            /**
             * save data
            **/
            $status = $this->model->save($data, $addNew);
            if($status === true){
                //del old file
                if($data['DOCK_IMGD_NEW'] != null){
                    if($data['DOCK_IMGD_NEW'] != $data['DOCK_IMGD_OLD']){
                        $path = "uploads/images/com_pgb/".$data['DOCK_IMGD_OLD'];
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                    }
                }
                //del old file
                if($data['DOCK_IMGW_NEW'] != null){
                    if($data['DOCK_IMGW_NEW'] != $data['DOCK_IMGW_OLD']){
                        $path = "uploads/images/com_pgb/".$data['DOCK_IMGW_OLD'];
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                    }
                }
                //del old file
                if($data['DOCK_IMGS_NEW'] != null){
                    if($data['DOCK_IMGS_NEW'] != $data['DOCK_IMGS_OLD']){
                        $path = "uploads/images/com_pgb/".$data['DOCK_IMGS_OLD'];
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                    }
                }
                $this->setRedirect('index.php?option=com_pgb');
            }else{

                if($status == 'empty'){
                    //required atleast 1 value
                    $error = "何も登録情報がありません";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
                } else {
                    //JError::raiseError(500, 'Error ! Can\'t save' );
                    $error = "予期せぬエラーが発生しました。登録ができませんでした。";
                    JRequest::setVar('task','');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
                }
                
            }
            
        }
        parent::display();
    }
    
    /* delete list center selected */
    public function remove(){
   	    $cid = JRequest::getVar('cid');
        
        /* remove imaage in server */
        foreach($cid as $value){
            //get image name
            $img = $this->getModel()->getImgFromOCBA_CODE($value);
            //Del Image
            if($img != null){
                //IMAGE 1
                if($img->DOCK_IMGD != null){
                    $path = "uploads/images/com_pgb/$img->DOCK_IMGD";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
                //IMAGE 2
                if($img->DOCK_IMGW != null){
                    $path = "uploads/images/com_pgb/$img->DOCK_IMGW";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
                //IMAGE 3
                if($img->DOCK_IMGS != null){
                    $path = "uploads/images/com_pgb/$img->DOCK_IMGS";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
            }
        }
        
        $this->model->remove($cid);
        $this->setRedirect('index.php?option=com_pgb');     
    }
    
    /* search center pop up */
     public function searchCenter(){
        $search['OCBA_NAME'] =  JRequest::getVar('OCBA_NAME');
        $search['ocba_code_old'] =  JRequest::getVar('ocba_code_old');
        
        $this->search_name = $search['OCBA_NAME'];
        $result = $this->getModel()->getAllListCenterEmpty($search);
        
        echo '
                <div class="the_scroll">
                   <table class="adminlist">
                		<thead>
                			<tr>
                				<th class="th30">
                					番号	
                                </th>
                                <th class="th70">
                                    名称
                                </th>
                			</tr>
        		          </thead>     		
                		<tbody>';
                      
                          if($result != null){
                            foreach($result as $k=>$row){
                 echo '            
                		  <tr class="row'.($k%2).'">
                			<td class="center">
                                <a href="javascript:void(0,0);" class="link_ocba_code" id="jformOCBA_CODE">'.$row->OCBA_CODE.'</a>
                            </td>
                			<td class="center">'.$row->OCBA_NAME.'</td>
                		  </tr>';
                        
                            }
                        }else{
                echo '      <tr>
                                <td class="center" colspan="2">データは見つかりませんでした。</td>
                            </tr>';
                      
                        }
                     
            	echo '</tbody>
                   
                	</table>
                 </div>';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    /* clear search center pop up */
    public function clearSearchCenter(){
        $search['ocba_code_old'] =  JRequest::getVar('ocba_code_old');
        $result = $this->getModel()->getAllListCenterEmpty($search);
        
        echo '
                <div class="the_scroll">
                    <table class="adminlist">
                		<thead>
                			<tr>
                				<th class="th30">
                					番号	
                                </th>
                                <th class="th70">
                                    名称
                                </th>
                			</tr>
        		          </thead>     		
                		<tbody>';
                      
                      
                        if($result != null){
                            foreach($result as $k=>$row){
                 echo '            
                		 <tr class="row'.($k%2).'">
                			<td class="center">
                                <a href="javascript:void(0,0);" class="link_ocba_code" id="jformOCBA_CODE">'.$row->OCBA_CODE.'</a>
                            </td>
                			<td class="center">'.$row->OCBA_NAME.'</td>
                		  </tr>';
                        
                            }
                        }else{
                echo '      <tr>
                                <td class="center" colspan="2">データは見つかりませんでした。</td>
                            </tr>';
                      
                        }
                     
            	echo '</tbody>
                   
                	</table>
                 </div>';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    /**
     * ajax get name with id
    **/
    public function ajax_get_name(){
        $ocba_code = JRequest::getVar('ocba_code');
        $model = $this->getModel();
        
        echo $model->centerName($ocba_code);        
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    /**
     * check center exist
    **/
    public function checkCenter(){
        $ocba_code = JRequest::getVar('ocba_code');
        $ocba_code_old = JRequest::getVar('ocba_code_old');
        $addNew = JRequest::getVar('addnew');
        if($addNew == 1){ $addNew = true;}else{ $addNew = false;}
        $model = $this->getModel();
        
        if($model->checkExistOCBA_CODE($ocba_code)){
            if($addNew == true){
                if($model->checkExistOCBA_PGB_NEW($ocba_code)){
                    //echo '<span style="color: red; ">KHONG THE THEM THONG TIN CHO TRUNG TAM NAY</span>';
                    echo '<span style="color: red; ">このセンター・クラブの情報を追加できません。</span><input type="hidden" name="check" id="check" value="0" />';
                }else{
                    //echo '<span style="color: green; ">CO THE THEM THONG TIN CHO TRUNG TAM NAY</span>';
                    echo '<span style="color: green; ">このセンター・クラブの情報を追加できます。</span><input type="hidden" name="check" id="check" value="1" />';
                }
            }else{
                if($model->checkExistOCBA_PGB_EDIT($ocba_code,$ocba_code_old)){
                    //echo '<span style="color: red; ">KHONG THE THEM THONG TIN CHO TRUNG TAM NAY</span>';
                    echo '<span style="color: red; ">このセンター・クラブの情報を追加できません。</span><input type="hidden" name="check" id="check" value="0" />';
                }else{
                    //echo '<span style="color: green; ">CO THE THEM THONG TIN CHO TRUNG TAM NAY</span>';
                    echo '<span style="color: green; ">このセンター・クラブの情報を追加できます。</span><input type="hidden" name="check" id="check" value="1" />';
                }
            }
        }else{
            //echo '<span style="color: red">KHONG TIM THAY TRUNG TAM</span>';
            echo '<span style="color: red">このセンター・クラブは見つかれません。</span><input type="hidden" name="check" id="check" value="0" />';
        }     
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    /**
     * AJAX search OCBA_CODE 
    **/
    public function searchOCBA_CODE(){
        $ocba_code_old = JRequest::getVar('ocba_code_old');
        $model = $this->getModel();
        $result = $model->getListOCBA_CODE($ocba_code_old);        
        foreach($result as $value){
            echo "$value->OCBA_CODE\n";
        }
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    
    
}